//
//  ConfigGlobal.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/7/21.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

enum FontLevel : Int {
    case normal = 0
    case middle = 1
    case large = 2
}

var RootIP:YCRootIP?

var userInfo:YCUserInfo?
//记录app开始运行的时间
var appActiveTime: Date?

var captuvoIsOn:Bool = false

var lastestVersionInfo: [String : String]?

class ConfigGlobal: NSObject {
    
    //MARK: - 初始化项目配置
    class func installConfig(){
        RootIP = self.getRootIP()
        userInfo = self.getUserInfo()
        _ = CaptuvoDecoderManager.shareCaptuvoDecoderManager
        _ = AVQRCodeViewManager.shareAVQRCodeViewManager
        AFNetworkReachabilityManager.shared().startMonitoring()
        _ = viewCacheManager.shareManager
        SVProgressHUD.setMinimumDismissTimeInterval(1.5)
    }
    
    class func saveConfig(){
        let path = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("userInfo")
        NSKeyedArchiver.archiveRootObject(userInfo!, toFile: path)
        
    }

    //MARK: - 账户管理
    class func getUserInfo() -> (YCUserInfo){ //获取账户
        let path = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("userInfo")
        guard let userInfo1 = NSKeyedUnarchiver.unarchiveObject(withFile: path) else{
            userInfo = YCUserInfo()
            return userInfo!
        }
        let userInfo2:YCUserInfo =  userInfo1 as! YCUserInfo
        return userInfo2
    }
    //MARK: - IP地址配置
    //取地址
    class func getRootIP() -> (YCRootIP){
        let path = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("RootIP")
        guard let ip = NSKeyedUnarchiver.unarchiveObject(withFile: path) else{
            return YCRootIP()
        }
        let IP:YCRootIP =  ip as! YCRootIP
        return IP
    }
    //存地址
    class func saveRootIP(_ IP:String , name:String){
        let path = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("RootIP")
        RootIP = YCRootIP(IP: IP, Name: name)
        NSKeyedArchiver.archiveRootObject(RootIP!, toFile: path)
    }
    
}

class YCUserInfo: NSObject,NSCoding {
    var isAutomicUpdate: Bool = true
    var userName:String = ""
    var userPassWord:String = ""
    var fontLevel:FontLevel = .normal
    var mainMenuDataArray: [[YunCangModel]]?
    var favoriteItems: [YunCangModel]?{
        didSet{
            NotificationCenter.default.post(name: Notification.Name(rawValue: "favoriteItemsChanged"), object: nil, userInfo: nil)//发送数据改变的通知
        }
    }
    
    convenience init(userName:String, userPassWord:String){
        self.init()
        self.userName = userName
        self.userPassWord = userPassWord
    }
    override init() {
        super.init()
    }
    func encode(with aCoder:NSCoder){
        aCoder.encode(userName, forKey: "userName")
        aCoder.encode(userPassWord, forKey: "userPassWord")
        aCoder.encode(fontLevel.rawValue, forKey: "fontLevelStore")
        aCoder.encode(isAutomicUpdate, forKey: "isAutomicUpdate")
    }
    internal required init?(coder aDecoder: NSCoder){
        super.init()
        self.userName = (aDecoder.decodeObject(forKey: "userName") as? String) ?? ""
        self.userPassWord = (aDecoder.decodeObject(forKey: "userPassWord") as? String) ?? ""
        self.fontLevel = FontLevel(rawValue: (aDecoder.decodeObject(forKey: "fontLevelStore") as? Int) ?? 0)!
        self.isAutomicUpdate = aDecoder.decodeBool(forKey: "isAutomicUpdate") 
    }
}
class YCRootIP: NSObject,NSCoding {
    var rootIP:String?
    var IPName:String?
    
    convenience init(IP:String, Name:String){
        self.init()
        self.rootIP = IP
        self.IPName = Name
    }
    override init() {
        super.init()
    }
    
    func encode(with aCoder:NSCoder){
        self.yy_modelEncode(with: aCoder)
    }
    internal required init?(coder aDecoder: NSCoder){
        super.init()
        self.yy_modelInit(with: aDecoder)
    }
}









